package com.dgiot.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.AssetManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.widget.TextView;

import com.dgiot.myapplication.databinding.ActivityMainBinding;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'myapplication' library on application startup.
    static {
        System.loadLibrary("myapplication");
    }

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        GLFrameSurface mGLSurface = binding.glFrameSurface;
        mGLSurface.setEGLContextClientVersion(2);

        GLFrameRenderer mGLFRenderer = new GLFrameRenderer(new ISimplePlayer() {
            @Override
            public void onPlayStart() {

            }

            @Override
            public void onReceiveState(int state) {

            }
        },mGLSurface, null);
        mGLSurface.setRenderer(mGLFRenderer);

        AssetManager manager = this.getAssets();
        new Thread(){
            @Override
            public void run() {
                try{
                    InputStream inputStream = null;
                    int width = 1280;
                    int height = 720;
                    mGLFRenderer.update(width, height);
                    int size = 2;

                    inputStream = manager.open("output.yuv");
                    Log.d("Mainaclog","size="+size);
                    while (size > 1) {
                        byte[] y = new byte[width * height];
                        size = inputStream.read(y);
                        if (size == 0) {
                            break;
                        }

                        byte[] u = new byte[width * height / 4];
                        size = inputStream.read(u);
                        if (size == 0) {
                            break;
                        }
                        byte[] v = new byte[width * height / 4];
                        size = inputStream.read(v);
                        if (size == 0) {
                            break;
                        }
                        mGLFRenderer.update(y, u, v);
                        Log.d("Mainaclog","----"+size);
                        SystemClock.sleep(40);
                    }
                    inputStream.close();


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        // Example of a call to a native method
//        TextView tv = binding.sampleText;
//        tv.setText(stringFromJNI());
    }
    /**
     * A native method that is implemented by the 'myapplication' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();


}